/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.pojo.Customer;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;



/**
 *
 * @author DucThien
 */
public class customerDAO extends AbstractDAO<Customer, String> {

    @Override
    protected Class getPOJOClass() {
       return Customer.class;
    }
    
    public String HashPassword(String password){
        String hashtext = "";
        try {
            String plaintext = password;
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.reset();
            m.update(plaintext.getBytes());
            byte[] digest = m.digest();
            BigInteger bigInt = new BigInteger(1,digest);
            hashtext = bigInt.toString(16);
            // Now we need to zero pad it if you actually want the full 32 chars.
            while(hashtext.length() < 32 ){
                hashtext = "0"+hashtext;
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(customerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return hashtext;
    }
    
    public boolean checkLogin(String username , String password){
        Session session = HibernateUtil.getSessionFactory().openSession();      
        boolean result = false;
        try{ 
            String hql = "FROM Customer c "
                        +"WHERE c.username=:username";
            Query query = session.createQuery(hql);
            query.setString("username",username);
            List<Customer> list = query.list();
            if(list.size() > 0){                
                result = (list.get(0).getPassword() == null ? (HashPassword(password)) == null : list.get(0).getPassword().equals(HashPassword(password))) ? true : false ;
            }
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }
        return result; 
    }
    
    public boolean checkEmailExists(String email){
        Session session = HibernateUtil.getSessionFactory().openSession();      
        try{ 
            String hql = "FROM Customer c "
                        +"WHERE c.email=:email";
            Query query = session.createQuery(hql);
            query.setString("email",email);
            List<Customer> list = query.list();
            if(list.size() > 0){                
                return true;
            }
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }
        return false; 
    }
    
    public boolean checkUsername(String username){
        Session session = HibernateUtil.getSessionFactory().openSession();      
        try{ 
            String hql = "FROM Customer c "
                        +"WHERE c.username=:username";
            Query query = session.createQuery(hql);
            query.setString("username",username);
            List<Customer> list = query.list();
            if(list.size() > 0){                
                return false;
            }
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }
        return true; 
    }
    
    public Customer getCustomerByCustomerId(int customer_id){
        List<Customer> list = null;
        Session session = HibernateUtil.getSessionFactory().openSession();      
        try{ 
            String hql = "FROM Customer c "
                        +"WHERE c.customerId=:customerID";
            Query query = session.createQuery(hql);
            query.setInteger("customerID",customer_id);
            list = query.list();
            if(list.isEmpty()){                
                return null;
            }
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }
        return list.get(0);     
    }
    public Customer getCustomerByUsername(String username){
        List<Customer> list = null;
        Session session = HibernateUtil.getSessionFactory().openSession();      
        try{ 
            String hql = "FROM Customer c "
                        +"WHERE c.username=:userName";
            Query query = session.createQuery(hql);
            query.setString("userName",username);
            list = query.list();
            if(list.isEmpty()){                
                return null;
            }
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }
        return list.get(0);     
    }
    public List<Customer> getCustomerByListCustomerId(String list_customer_id){ // chua test
        List<Customer> list = null;
        Session session = HibernateUtil.getSessionFactory().openSession();      
        try{ 
            String hql = "FROM Customer c "
                        +"WHERE c.customerId in :listCustomerID";
            Query query = session.createQuery(hql);
            query.setString("listCustomerID","("+list_customer_id+")");
            list = query.list();
            if(list.isEmpty()){                
                return null;
            }
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }
        return list;     
    }
    
}

       
